package com.dkd.mange.service.impl;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.dkd.common.utils.DateUtils;
import com.dkd.common.utils.StringUtils;
import com.dkd.common.utils.uuid.UUIDUtils;
import com.dkd.mange.domain.Channel;
import com.dkd.mange.domain.Node;
import com.dkd.mange.domain.VmType;
import com.dkd.mange.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dkd.mange.mapper.VendingMachineMapper;
import com.dkd.mange.domain.VendingMachine;
import org.springframework.transaction.annotation.Transactional;

 /**
 * 设备Service业务层处理
 * @author GuihaoLv
 * @date 2024-10-13
 */
@Service
public class VendingMachineServiceImpl implements IVendingMachineService 
{
    @Autowired
    private VendingMachineMapper vendingMachineMapper;
    @Autowired
    private INodeService nodeService;
    @Autowired
    private IVmTypeService vmTypeService;
    @Autowired
    private IChannelService channelService;



     /**
     * 查询设备
     * @param id 设备主键
     * @return 设备
     */
    @Override
    public VendingMachine selectVendingMachineById(Long id)
    {
        return vendingMachineMapper.selectVendingMachineById(id);
    }


     /**
     * 查询设备列表
     * @param vendingMachine 设备
     * @return 设备
     */
    @Override
    public List<VendingMachine> selectVendingMachineList(VendingMachine vendingMachine)
    {
        return vendingMachineMapper.selectVendingMachineList(vendingMachine);
    }




    /**
    * 新增设备
    * @param vendingMachine 设备
    * @return 结果
    */
   @Override
   @Transactional
   public int insertVendingMachine(VendingMachine vendingMachine)
   {
       //新增售货机编号
       String innerCode= UUIDUtils.getUUID();
       vendingMachine.setInnerCode(innerCode);
       //查询出该设备所属的类型
       VmType vmType = vmTypeService.selectVmTypeById(vendingMachine.getVmTypeId());
       vendingMachine.setChannelMaxCapacity(vmType.getChannelMaxCapacity());
       vendingMachine.setCreateTime(DateUtils.getNowDate());
       vendingMachine.setUpdateTime(DateUtils.getNowDate());
       //查询出点位
       Node node = nodeService.selectNodeById(vendingMachine.getNodeId());
       BeanUtil.copyProperties(node, vendingMachine, "id");
       vendingMachine.setAddr(node.getAddress());
       //保存数据
       int result=vendingMachineMapper.insertVendingMachine(vendingMachine);


       //新增货道
       List<Channel> channelList=new ArrayList<>();
       for(int i=1;i<=vmType.getVmRow();i++){
           for (int j=1;j<=vmType.getVmCol();j++){
                Channel channel=new Channel();
                String channelCode=UUIDUtils.getUUID();
                channel.setChannelCode(i+"-"+j);
                channel.setVmId(vendingMachine.getId());
                channel.setMaxCapacity(vmType.getChannelMaxCapacity());
                channel.setInnerCode(innerCode);
                channel.setCreateTime(DateUtils.getNowDate());
                channel.setUpdateTime(DateUtils.getNowDate());
                channelList.add(channel);
           }
       }
       //2-4 批量新增
        channelService.batchInsertChannel(channelList);
        return result;
   }






    /**
     * 修改设备管理
     *
     * @param vendingMachine 设备管理
     * @return 结果
     */
    @Override
    public int updateVendingMachine(VendingMachine vendingMachine)
    {
        if (vendingMachine.getNodeId()!=null) {
            // 查询点位表，补充：区域、点位、合作商等信息
            Node node = nodeService.selectNodeById(vendingMachine.getNodeId());
            BeanUtil.copyProperties(node,vendingMachine,"id");// 商圈类型、区域、合作商
            vendingMachine.setAddr(node.getAddress());// 设备地址
            vendingMachine.setPolicyId(vendingMachine.getPolicyId());
        }
        vendingMachine.setUpdateTime(DateUtils.getNowDate());// 更新时间
        return vendingMachineMapper.updateVendingMachine(vendingMachine);
    }





     /**
     * 批量删除设备
     * @param ids 需要删除的设备主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineByIds(Long[] ids)
    {
        return vendingMachineMapper.deleteVendingMachineByIds(ids);
    }

     /**
     * 删除设备信息
     * @param id 设备主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineById(Long id)
    {
        return vendingMachineMapper.deleteVendingMachineById(id);
    }
}
